package org.example.springboot.service;


import jakarta.annotation.Resource;
import org.example.springboot.entity.BD;
import org.example.springboot.mapper.ShowMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShowService {

    @Resource
    private ShowMapper showMapper;

    public List<BD> getAllData() {
        return showMapper.list();
    }

    public List<Map<String, Object>> getBookSortNumber() {
        return showMapper.bookSortNumber();
    }


    // 获取分类占比
    public List<Map<String, Object>> getCategoryPercentage() {
        return showMapper.selectCategoryPercentage();
    }

    // 获取分类人气排行
    public List<Map<String, Object>> getCategoryPopularityRanking() {
        return showMapper.selectCategoryPopularityRanking();
    }


    // 获取分类人气关系分析
    public Map<String, Object> getCategoryPopularityRelation() {
        // 原始数据：每个分类的 avg/max/min 人气值
        List<Map<String, Object>> rawData = showMapper.selectCategoryPopularityRelation();

        // 构建雷达图指标维度
        List<Map<String, Object>> indicators = new ArrayList<>();
        // 构建雷达图数据系列
        List<Map<String, Object>> seriesData = new ArrayList<>();

        // 动态计算每个指标的最大值（示例：取最大值的 1.2 倍，确保图表显示留白）
        double maxAvg = 0, maxMax = 0, maxMin = 0;
        for (Map<String, Object> item : rawData) {
            double avg = convertToBigDecimal(item.get("avgPopularity")).doubleValue();
            double max = convertToBigDecimal(item.get("maxPopularity")).doubleValue();
            double min = convertToBigDecimal(item.get("minPopularity")).doubleValue();
            maxAvg = Math.max(maxAvg, avg);
            maxMax = Math.max(maxMax, max);
            maxMin = Math.max(maxMin, min);
        }

        // 设置雷达图指标维度
        indicators.add(createIndicator("平均人气", maxAvg * 1.2));
        indicators.add(createIndicator("最高人气", maxMax * 1.2));
        indicators.add(createIndicator("最低人气", maxMin * 1.2));

        // 构建每个分类的数据点
        for (Map<String, Object> item : rawData) {
            String category = (String) item.get("category");
            double avg = convertToBigDecimal(item.get("avgPopularity")).doubleValue(); // 修复
            double max = convertToBigDecimal(item.get("maxPopularity")).doubleValue(); // 修复
            double min = convertToBigDecimal(item.get("minPopularity")).doubleValue(); // 修复

            Map<String, Object> dataPoint = new HashMap<>();
            dataPoint.put("name", category);
            dataPoint.put("value", new double[]{avg, max, min});
            seriesData.add(dataPoint);
        }

        // 返回结构化数据
        Map<String, Object> result = new HashMap<>();
        result.put("indicators", indicators);
        result.put("seriesData", seriesData);
        return result;
    }

    private BigDecimal convertToBigDecimal(Object value) {
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        } else if (value instanceof Integer) {
            return new BigDecimal((Integer) value);
        }
        throw new ClassCastException("Unsupported type for conversion to BigDecimal.");
    }
    private Map<String, Object> createIndicator(String name, double max) {
        Map<String, Object> indicator = new HashMap<>();
        indicator.put("name", name);
        indicator.put("max", Math.ceil(max)); // 向上取整，避免小数
        return indicator;
    }

    // 获取字数排行前12
    public List<Map<String, Object>> getWordCountTop12() {
        return showMapper.selectWordCountTop12();
    }

    public List<Map<String, Object>> getPopularityTop12() {
        return showMapper.selectPopularityTop12();
    }
}
